package com.mlm.mlmapp.activity;


import android.widget.TextView;

import com.joanzapata.pdfview.PDFView;
import com.joanzapata.pdfview.listener.OnPageChangeListener;
import com.mlm.mlmapp.R;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import static java.lang.String.format;

/**
 * Activity which displays a login screen to the user, offering registration as
 * well.
 */
@EActivity(R.layout.activity_pdf)
public class PDFActivity extends BaseActivity implements OnPageChangeListener {
	public static final String SAMPLE_FILE = "registration.pdf";

	String pdfName = SAMPLE_FILE;

	Integer pageNumber = 1;
	@ViewById
	PDFView pdfView;

	@AfterViews
	void afterViews() {

		String title = getTitle().toString();
		if (!"".equals(title)) {
			((TextView) this.findViewById(R.id.tv_title)).setText(title);
		}
		display(pdfName, false);
	}

	private void display(String assetFileName, boolean jumpToFirstPage) {
		if (jumpToFirstPage) pageNumber = 1;
		setTitle(pdfName = assetFileName);

		pdfView.fromAsset(assetFileName)
				.defaultPage(pageNumber)
				.onPageChange(this)
				.load();
	}
	@Override
	public void onPageChanged(int page, int pageCount) {
		pageNumber = page;
		setTitle(format("%s %s / %s", pdfName, page, pageCount));
	}

	@Click
	void btn_back(){
		this.onBackPressed();
	}
	private boolean displaying(String fileName) {
		return fileName.equals(pdfName);
	}
}
